package com.example.memurbac_service.dao;

import com.example.memurbac_service.pojo.Auth;
import com.example.memurbac_service.pojo.Auth;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.Date;

import java.util.List;

@Repository
@Mapper
public interface AuthMapper {


    @Select("select * from t_auth where auth_status=0 order by auth_createtime desc")
    @Results({
            @Result(property = "id",column = "id"),
            @Result(property = "auth_name",column = "auth_name"),
            @Result(property = "auth_desc",column = "auth_desc"),
            @Result(property = "auth_status",column = "auth_status"),
            @Result(property = "auth_createtime",column = "auth_createtime"),
            @Result(property = "auth_modiftime",column = "auth_modiftime"),
    })
    List<Auth> SelectAuth();

    @Select("select a.*,c.memu_name from t_auth a,auth_memu b,t_memu c where a.id=b.auth_id and b.memu_id=c.id and auth_name like '%${auth_name}%' order by auth_createtime desc")
    @Results({
            @Result(property = "id",column = "id"),
            @Result(property = "auth_name",column = "auth_name"),
            @Result(property = "memu_name",column = "memu_name"),
            @Result(property = "auth_desc",column = "auth_desc"),
            @Result(property = "auth_status",column = "auth_status"),
            @Result(property = "auth_createtime",column = "auth_createtime"),
            @Result(property = "auth_modiftime",column = "auth_modiftime"),

    })
    List<Auth> getSelectAuth(String auth_name);

    @Insert("insert into t_auth(auth_name,auth_desc,auth_status,auth_createtime,auth_modiftime) values(#{auth_name},#{auth_desc},0,now(),now())")
    @Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
    int AddAuth(Auth auth);

    @Delete("delete from t_auth  where id=#{id};"+
            "delete from auth_memu  where auth_id=#{id}")
    int DelAuth(int id);

    @Update("update t_auth set auth_name=#{param2},auth_desc=#{param3},auth_modiftime=now() where id=#{param1};"+
            "update auth_memu set memu_id=#{param4}  where auth_id=#{param1}")

    int UpdAuth(int id, String auth_name, String auth_desc,int memuid);


    //停用权限
    @Update("update t_auth set auth_status=1 where id=#{authid}")
    int offauth(int authid);

    //启用权限
    @Update("update t_auth set auth_status=0 where id=#{authid}")
    int onauth(int authid);

    //根据id查询权限信息
    @Select("select a.*,b.memu_id from t_auth a,auth_memu b  where a.id=b.auth_id and a.id = #{authid} order by auth_createtime desc ")
    @Results({
            @Result(property = "id",column = "id"),
            @Result(property = "auth_name",column = "auth_name"),
            @Result(property = "memuid",column = "memu_id"),
            @Result(property = "auth_desc",column = "auth_desc"),
            @Result(property = "auth_status",column = "auth_status"),
            @Result(property = "auth_createtime",column = "auth_createtime"),
            @Result(property = "auth_modiftime",column = "auth_modiftime")

    })
    Auth selectidauth(int authid);


    //添加权限菜单
    @Insert("insert into auth_memu values(#{id},#{memuid})")
    int AddAuthmemu(Auth auth);

}
